// pages/user/business-info/business-info.js
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
let app = getApp()
const STYLE_TEXT = {
  0: '全部',
  1: '站点毛利结算',
  2: '站点门店佣金发放',
  3: '站点自有供应商结算',
  4: '售后平台给站点货款',
  5: '平台给站点充值',
  6: '平台给站点扣款',
  7: '站点给用户或门店充值',
  8: '站点给用户或门店扣钱',
  9: '站点提现',
  10: '站点提现失败返回',
  11: '站点结算扣除优惠券金额',
  12: '站点结算扣除积分抵扣金额',
  13: '用户购买vip充值',
  14: '用户购买会员卡充值',
}
const STATUS_ORDER = {
  0: '全部',
  1: '未支付',
  2: '待发货',
  3: '待收货',
  4: '已收货',
  5: '待退款',
  6: '售后中',
  7: '已完成',
  8: '已取消',
}
const PAY_STATE = {
  0: '全部',
  1: '未支付',
  2: '支付中',
  3: '支付完成',
  4: '支付失败',
  5: '代付申请',
  6: '待付中',
  7: '代付成功',
  8: '代付失败',
  9: '退款申请',
  10: '退款中',
  11: '退款成功',
  12: '退款失败',
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    tabActive: 0,
    listList: [],
    centerObj: '',
    sumObj: '',
    isShowCenterInfo: false,
    cashMonitor: false,//提现弹框
    cashValue: '',//提现金额
    reqStaDate: '',//开始时间
    reqEndDate: '',//结束时间
    reqPayState: '',//支付状态
    reqStatus: '',//订单状态

    isShowWater: false,
    water: '',
    waterIndex: 0,
    STYLE_TEXT,
    waterColumns: Object.values(STYLE_TEXT),

    isShowOrder: false,
    order: '',
    orderIndex: '',
    STATUS_ORDER,
    orderColumns: Object.values(STATUS_ORDER),

    isShowPay: false,
    pay: '',
    payIndex: '',
    PAY_STATE,
    payColumns: Object.values(PAY_STATE),

    currPage: 1,
    perPage: 5,
    isEmpty: false,//true  无数据
    noMore: false,
  },

  //获取列表数据
  getInitData(isPage){//isPage=true下一页，isPage=false第一页
    let _this = this;
    if(_this.data.tabActive == 0){
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/center/amountLogs',
        data: {
          mid: _this.data.user.id,
          start: _this.data.reqStaDate,
          end: _this.data.reqEndDate,
          flowType: _this.data.waterIndex,
          offset: (_this.data.currPage - 1) * _this.data.perPage,
          limit: _this.data.perPage
        },
        load: 1
      }).then(res =>{
        let tempList = res.data.data

        if (isPage) { //下一页的数据拼接在原有数据后面
          _this.setData({
            listList: _this.data.listList.concat(tempList)
          })
        } else {//第一页数据直接赋值
          _this.setData({
            listList: tempList,
            isEmpty: !tempList.length > 0,
          })
        }
        //如果返回的数据为空，那么就没有下一页了
        if (tempList.length == 0) {
          _this.setData({ noMore: true})
        }
      })
    } else if(_this.data.tabActive == 1){
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/getReportProductSaleList',
        data: {
          mid: _this.data.user.id,
          payStartTime: _this.data.reqStaDate,
          payEndTime: _this.data.reqEndDate,
          status: _this.data.orderIndex,
          payState: _this.data.payIndex,
          offset: (_this.data.currPage - 1) * _this.data.perPage,
          limit: _this.data.perPage
        },
        load: 1
      }).then(res =>{
        let tempList = res.data.data

        if (isPage) { //下一页的数据拼接在原有数据后面
          _this.setData({
            listList: _this.data.listList.concat(tempList)
          })
        } else {//第一页数据直接赋值
          _this.setData({
            listList: tempList,
            isEmpty: !tempList.length > 0,
          })
        }
        //如果返回的数据为空，那么就没有下一页了
        if (tempList.length == 0) {
          _this.setData({ noMore: true})
        }
      })
    }

  },

  //销量总和总金额
  sumSum () {
    let _this = this
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/getReportProductSaleTotal',
      data: {
        mid: _this.data.user.id,
        payStartTime: _this.data.reqStaDate,
        payEndTime: _this.data.reqEndDate,
        payState: _this.data.reqPayState,
        status: _this.data.reqStatus
      },
      load: 1
    }).then(res => {
      _this.setData({
        sumObj: res.data.data
      })
    })
  },
  //商户后台信息
  centerInfo () {
    let _this = this
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/center/info',
      data: {
        mid: _this.data.user.isCentre == 1 ? _this.data.user.id : ''
      },
      load: 1
    }).then(res => {
      _this.setData({
        centerObj: res.data.data
      })
    })
  },

  //提现
  cashCloseAndSubmit(event){
    let _this = this;
    if(event.detail == 'confirm'){//阻止关闭
      _this.setData({
        cashMonitor: true
      })
      if(_this.data.cashValue == '') {
        wx.showToast({
          title: '请填写提交金额',
          icon: 'none'
        });
        return;
      };
      app.requestUrl({
        method: 'POST',
        url: '/wxapi/applyCash',
        data: {
          centreId: app.globalData.loginCentreId,
          mid: _this.data.user.id,
          memName: _this.data.user.nickname,
          cashAmount: _this.data.cashValue,
        }
      }).then(res =>{
        if(res.data.code == 0){
          Toast('提现成功，请等待审核');
          _this.setData({
            cashMonitor: false,
            cashValue: ''
          })
        } else {
          wx.showToast({title: res.data.msg, icon: 'none'})
        }
      })
    } else {

    }
  },
  cashValueBlur(event){
    this.setData({cashValue: event.detail.value})
  },
  //提现弹框
  cashMonitor(){
    let _this = this;
    _this.setData({
      cashMonitor: !_this.data.cashMonitor
    })
  },

  //选择时间
  showDateClick () {
    this.setData({
      isShowDate: true,
    })
  },
  //接收时间
  collectDate (event) {
    this.setData({
      isShowDate: false,
      reqStaDate: event.detail.sta,
      reqEndDate: event.detail.end,
    })
    this.getInitData(false)
  },


  showCenterInfoMonitor () {
    let _this = this
    _this.setData({
      isShowCenterInfo: !_this.data.isShowCenterInfo
    })
  },

  waterMonitor () {
    let _this = this
    _this.setData({
      isShowWater: !_this.data.isShowWater
    })
  },
  waterConfirm (event) {
    let _this = this
    console.log(event.detail)
    _this.setData({
      water: event.detail.value,
      waterIndex: event.detail.index
    })
    _this.getInitData(false);
    _this.waterMonitor();
  },
  orderMonitor () {
    let _this = this
    _this.setData({
      isShowOrder: !_this.data.isShowOrder
    })
  },
  orderConfirm (event) {
    let _this = this
    _this.setData({
      orderIndex: event.detail.index==0 ? '' : event.detail.index,
      order: event.detail.value
    })
    _this.getInitData(false);
    _this.orderMonitor();

  },
  payMonitor () {
    let _this = this
    _this.setData({
      isShowPay: !_this.data.isShowPay
    })
  },
  payConfirm (event) {
    let _this = this
    _this.setData({
      payIndex: event.detail.index==0 ? '' : event.detail.index-1,
      pay: event.detail.value
    })
    _this.getInitData(false);
    _this.payMonitor()
  },

  //tab切换调用组件方法获取数据
  tabChange (event) {
    this.setData({
      reqStaDate: '',
      reqEndDate: '',
      tabActive: event.detail.index
    })
    this.getInitData();
  },

  toBusinessCashList(){
    wx.navigateTo({
      url: '/sub/user/business-cash-list/business-cash-list'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this

    _this.setData({
      user: app.getUser()
    })
    _this.centerInfo()
    _this.sumSum()
    _this.getInitData(false)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noMore) {
      this.setData({
        currPage: this.data.currPage + 1
      })
      this.getInitData(true)//传true代表进行下一页
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})